from random import randint
from time import sleep

from selenium import webdriver
from selenium.webdriver.support.select import Select

driver = webdriver.Firefox()
# 打开登录页面
driver.get('http://localhost/wdkkk/index.asp')

# 完成登录
ipts = driver.find_elements_by_tag_name('input')
ipts[0].send_keys('admin')
ipts[1].send_keys('admin')
ipts[2].click()

sleep(2)
# 从当前默认位置切换到菜单框架中
driver.switch_to.frame('left')

# 点击菜单 系统设置
sysset_css = '.menuall > tbody:nth-child(1) > tr:nth-child(13) > td:nth-child(1)'
driver.find_element_by_css_selector(sysset_css).click()

sleep(2)
# 继续点击：计量单位管理
danwei_css = '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(7) > td:nth-child(1)'
driver.find_element_by_css_selector(danwei_css).click()
sleep(2)

# 切换原始位置
driver.switch_to.default_content()
# 切到右侧框架
driver.switch_to.frame('right')
sleep(2)

# 点击全选按钮
ckbx_css='#chkall'
driver.find_element_by_css_selector(ckbx_css).click()
# 点击删除按钮
bt_css = 'input.button:nth-child(2)'
driver.find_element_by_css_selector(bt_css).click()
# 点击确认按钮
driver.switch_to.alert.accept()
sleep(2)


bt_css = 'input.button:nth-child(1)'
driver.find_element_by_css_selector(bt_css).click()
sleep(2)
# 输入单位名称
driver.find_element_by_name('danwei').send_keys('吨')
# 点击确认按钮
driver.find_element_by_name('submit').click()
sleep(2)
# 处理弹框
driver.switch_to.alert.accept()
sleep(2)





# 切换原始位置
driver.switch_to.default_content()
driver.switch_to.frame('left')
sysset_css = '.menuall > tbody:nth-child(1) > tr:nth-child(13) > td:nth-child(1)'
driver.find_element_by_css_selector(sysset_css).click()


sleep(2)
# 继续点击：添加大类
dalei_css = '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(5) > td:nth-child(1) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(2)'
driver.find_element_by_css_selector(dalei_css).click()
sleep(2)



# -------------添加大类------------------------
# 切换原始位置
driver.switch_to.default_content()
# 切到右侧框架
driver.switch_to.frame('right')
sleep(2)


bt_css = 'input.button:nth-child(1)'
driver.find_element_by_css_selector(bt_css).click()
sleep(2)

driver.find_element_by_name('bigclass').send_keys('吃的')

driver.find_element_by_name('submit').click()
sleep(2)
driver.switch_to.alert.accept()
sleep(2)

# -------------随机添加小类-----------------
# 切换原始位置
driver.switch_to.default_content()
# 切换左侧框架点击小类添加
driver.switch_to.frame('left')
sysset_css = '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(6) > td:nth-child(1) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(2)'
driver.find_element_by_css_selector(sysset_css).click()


# 切换到原始位置再到右框架
driver.switch_to.default_content()
driver.switch_to.frame('right')
sleep(2)

xllb = Select(driver.find_element_by_name('bigclass'))
lst_count = len(xllb.options)
rand = randint(0,lst_count-1)
xllb.select_by_index(rand)
xllb.select_by_index(rand)

bt_css = 'input.button:nth-child(1)'
driver.find_element_by_css_selector(bt_css).click()
sleep(2)
driver.find_element_by_name('smallclass').send_keys('肥宅快乐X')
driver.find_element_by_name('submit').click()
sleep(2)
driver.switch_to.alert.accept()
sleep(2)

# ------------quit-----------------------
# 切换原始位置
driver.switch_to.default_content()

# 切换到顶部框架
driver.switch_to.frame('topFrame')
driver.find_element_by_link_text('安全退出').click()
sleep(2)
driver.switch_to.alert.accept()
sleep(2)
# 关闭浏览器
driver.quit()





